<template>
  <div class="container">
    <!-- 固定导航 -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="老默"
    ></van-nav-bar>

    <!-- 聊天主体区域 -->
    <div class="chat-list">
      <div v-for="(obj, i) in list" :key="i">
        <!-- 左侧是机器人小思 -->
        <div class="chat-item left" v-if="obj.name === 'xs'" ref="msgFn">
          <van-image
            fit="cover"
            round
            src="https://img.yzcdn.cn/vant/cat.jpeg"
          />
          <div class="chat-pao">{{ obj.msg }}</div>
        </div>

        <!-- 右侧是当前用户 -->
        <div class="chat-item right" v-else ref="msgFn">
          <div class="chat-pao">{{ obj.msg }}</div>
          <van-image fit="cover" round :src="$route.query.imag" />
        </div>
      </div>
    </div>

    <!-- 对话区域 -->
    <div class="reply-container van-hairline--top">
      <van-field
        placeholder="说点什么..."
        v-model.trim="msg"
        @keyup.enter="handleSend"
      >
        <template #button>
          <span style="font-size: 12px; color: #999" @click="handleSend"
            >提交</span
          >
        </template>
      </van-field>
    </div>
  </div>
</template>

<script>
import { io } from "socket.io-client";
let socket = null;
export default {
  name: "Chat",
  data() {
    return {
      msg: "",
      list: [
        { name: "xs", msg: "Hi,我是小思,我能帮你什么呢" },
        { name: "me", msg: "我能帮你什么呢" },
      ],
    };
  },
  activated() {
    //与服务器建立链接
    socket = io("http://toutiao.itheima.net", {
      query: {
        token: this.$store.state.user.token,
      },
      transports: ["websocket"],
    });
    socket.on("connect", () => {
      socket.on("message", (data) => {
        this.list.push({
          name: "xs",
          msg: data.msg,
        });
        // 调用scroll()方法
        this.scroll();
      });
    });
    // 用on 方法接收服务器发送的信息
  },
  destroyed() {
    // 关闭连接
    socket.close();
    // 把变量socket设置为null，表示释放占用的内存空间
    socket = null;
  },
  methods: {
    // 发送消息
    handleSend() {
      // 非空校验
      if (!this.msg) return (this.msg = "");
      // 发送消息到服务器
      socket.emit("message", {
        msg: this.msg,
        timestamp: +new Date() + "",
      });
      this.list.push({ name: "me", msg: this.msg });
      this.msg = "";
      this.scroll();
      // 展示消息到页面
    },
    // 滚动到底部
    scroll() {
      this.$nextTick(() => {
        const lastItem = this.$refs.msgFn;
        lastItem[lastItem.length - 1].scrollIntoView({ behavior: "smooth" });
      });
    },
  },
};
</script>

<style lang="less" scoped>
.container {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  box-sizing: border-box;
  background: #fafafa;
  padding: 46px 0 50px 0;
  .chat-list {
    height: 100%;
    overflow-y: scroll;
    .chat-item {
      padding: 10px;
      .van-image {
        vertical-align: top;
        width: 40px;
        height: 40px;
      }
      .chat-pao {
        vertical-align: top;
        display: inline-block;
        min-width: 40px;
        max-width: 70%;
        min-height: 40px;
        line-height: 38px;
        border: 0.5px solid #c2d9ea;
        border-radius: 4px;
        position: relative;
        padding: 0 10px;
        background-color: #e0effb;
        word-break: break-all;
        font-size: 14px;
        color: #333;
        &::before {
          content: "";
          width: 10px;
          height: 10px;
          position: absolute;
          top: 12px;
          border-top: 0.5px solid #c2d9ea;
          border-right: 0.5px solid #c2d9ea;
          background: #e0effb;
        }
      }
    }
  }
}
.chat-item.right {
  text-align: right;
  .chat-pao {
    margin-left: 0;
    margin-right: 15px;
    &::before {
      right: -6px;
      transform: rotate(45deg);
    }
  }
}
.chat-item.left {
  text-align: left;
  .chat-pao {
    margin-left: 15px;
    margin-right: 0;
    &::before {
      left: -5px;
      transform: rotate(-135deg);
    }
  }
}
.reply-container {
  position: fixed;
  left: 0;
  bottom: 0;
  height: 44px;
  width: 100%;
  background: #f5f5f5;
  z-index: 9999;
}
</style>
